<template>
	<view class="content">
		<view class="header_top">
			<button class='bottom header_top_lf' @click="wxGetUserInfo" lang="zh_CN" v-if="!userinfo">
				<view class="header" v-if="!userinfo.userHeadPortrait">
					<image src="../../static/img/header.png" style="width: 60rpx;height: 68rpx;" mode=""></image>
				</view>
			
				<view class="login_type">
					<text>未登录</text>
					<text>登录之后更加精彩</text>
				</view>
			</button>
			<view class="header_top_lf" v-if="userinfo">
				
			    <view class="header" >
					<image :src="userinfo.userHeadPortrait" style="width: 100%;height: 100%;" mode=""></image>
				</view>
			    <view class="login_type">
					<text>{{userinfo.userName}}</text>
					<text>10积分等于1元</text>
				</view>
			    
			</view>

			<view class="jf_box" v-if="isSignin" @click="userSigninFun">
				<image src="../../static/img/date.png" style="width: 23rpx;height: 23rpx;" mode=""></image>
				<text>签到赚积分</text>
			</view>
			<view class="jf_box" style="width: 135rpx;" v-else>
				<image src="../../static/img/date.png" style="width: 23rpx;height: 23rpx;" mode=""></image>
				<text>已签到</text>
			</view>
		</view>
		<view class="score_nav" v-if="!userinfo">
			<view class="score_nav_item">
				<text>0</text>
				<text>钱包</text>
			</view>
			<view class="score_nav_item">
				<text>0</text>
				<text>积分</text>
			</view>
			<view class="score_nav_item">
				<text>0</text>
				<text>收藏</text>
			</view>
			<view class="score_nav_item">
				<text>0</text>
				<text>消息</text>
			</view>
		</view>
		<view class="score_nav" v-else>
			<view class="score_nav_item" @click="goMyMoney(userinfo.userBalance)">
				<text>{{userinfo.userBalance||0}}</text>
				<text>钱包</text>
			</view>
			<view class="score_nav_item" @click="goIntegral(userinfo.userIntegral||0)">
				<text>{{userinfo.userIntegral||0}}</text>
				<text>积分</text>
			</view>
			<view class="score_nav_item" @click="CollectionFun">
				<text>{{userinfo.userHouses||0}}</text>
				<text>收藏</text>
			</view>
			<view class="score_nav_item" @click="goMessage">
				<text>{{userinfo.userMessage||0}}</text>
				<text>消息</text>
			</view>
		</view>
		<view class="class_report">
			<view class="class_report_title">
				<image src="../../static/img/title_icon.png" style="width: 24rpx;height: 24rpx;" mode=""></image>
				<text>报课区</text>
			</view>
			<view class="class_report_content">
				<view class="class_report_item" @click="goKill(0)">
					<image src="../../static/img/ms_my.png" mode=""></image>
					<text>现时秒杀</text>
				</view>
				<view class="class_report_item"@click="goKill(1)">
					<image src="../../static/img/kj.png" mode=""></image>
					<text>一元砍价</text>
				</view>
				<view class="class_report_item" @click="goKill(2)">
					<image src="../../static/img/qct.png" mode=""></image>
					<text>全城团</text>
				</view>
				<view class="class_report_item" >
					<image src="../../static/img/djj.png" mode=""></image>
					<text>课程代金券</text>
				</view>
			</view>
		</view>
		<view class="use_methods">
			<view class="use_methods_item" v-for="(item,index) in bookList" :key="index" @click="goDetail(item.id)">
				<view class="use_methods_lf">
					<image :src="item.icon" mode=""></image>
					<text>{{item.bookTitle}}</text>
				</view>

				<uni-icons type="arrowright" color="#666666" size="16"></uni-icons>
			</view>

			<view class="use_methods_item">
				<view class="use_methods_lf">
					<image src="../../static/img/contant.png" mode=""></image>
					<text>联系我们</text>
				</view>

				<uni-icons type="arrowright" color="#666666" size="16"></uni-icons>
			</view>
			<view class="use_methods_item" @click="signOut">
				<view class="use_methods_lf">
					<image src="../../static/img/sign_out.png" mode=""></image>
					<text>退出登录</text>
				</view>

				<uni-icons type="arrowright" color="#666666" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: "",
				bookList: [], //文章列表
				isSignin: 1, //是否签到 0已签到 1未签到
				userinfo: "", //用户信息
				imageUrl: "",
				code: "",
				isCanUse: uni.getStorageSync('isCanUse'), //默认为true  记录当前用户是否是第一次授权使用的
				bookIcon: ['../../static/img/price.png', '../../static/img/jf_sy.png', '../../static/img/djj_tj.png'],
			}
		},
		onShow() {
			this.token = uni.getStorageSync('token');
			if (this.token) {
				this.userSignin();
				this.getUserinfo();
				this.bookListFun();
			}
			

		},
		methods: {
			//进入消息页面
			goMessage(){
				uni.navigateTo({
					url: '../mySub/message',
				});
			},
			//已抢购
			goKill(num){
				if(this.token){
					uni.navigateTo({
						url: '../mySub/snappedUp?num='+num,
					});
				}else{
					uni.showToast({
						title: '请授权登录!',
						icon: "none",
						duration: 2000,
					});
				}
				
			},
			//登录
			thirdLogin(code,userName) {
				this.$http.post('/api/dream/thirdLogin', {
					"areaCode": "",
					"code": code,
					"type": "0",
					"userName":userName,
				},'application/json').then((data) => {
					if (data.data.code == 200) {
						uni.showToast({
							title: '登录成功!',
							icon: "success",
							duration: 2000,
							success: () => {
								uni.setStorageSync('token', data.data.result);
								this.token=data.data.result;
								this.getUserinfo();
								this.userSignin()
							}
						});
					} else {
						uni.showToast({
							title: data.data.message,
							duration: 2000
						});
					}
				})
			},
			//微信授权
			wxGetUserInfo() {
				let _this = this;
				wx.getUserProfile({
					desc: "获取你的昵称、头像、地区及性别",
					success: function(data) {
						// _this.userinfo = data.userInfo;
						// console.log(data.userInfo)
						uni.setStorageSync('userinfo',JSON.stringify(_this.userinfo))
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes)
								_this.code = loginRes.code;
								_this.thirdLogin(loginRes.code,data.userInfo);


							},
						})
					},
					fail: (res) => {
						uni.showToast({
							title: '无法获取您的信息将影响您的以后的操作,请慎重!',
							icon: "none",
							duration: 2000,
						
						});
					}

				})


			},
			
			//获取用户信息
			getUserinfo() {
				this.$http.get('/api/user/userInfo').then((data) => {
					if (data.data.code == 200) {
						this.imageUrl = data.data.imageUrl;
						this.userinfo = data.data.result || {};
						console.log("this.userinfo:"+JSON.stringify(this.userinfo));
					} else {
						this.userinfo = {};
					}
				})
			},
			// login() {
			// 	uni.navigateTo({
			// 		url: '../mySub/login'
			// 	});
			// },
			bookListFun() {
				this.$http.get('/api/user/bookList/1').then((data) => {
					if (data.data.code == 200) {
						this.bookList = data.data.result || [];
						this.bookList.forEach((a, i) => {
							if (this.bookIcon[i]) {
								a.icon = this.bookIcon[i];
							}
						})
					} else {
						this.bookList = [];
					}
				})
			},
			//我的积分
			goIntegral() {
				uni.navigateTo({
					url: '../mySub/myIntegral?data=',
				});
			},
			//我的钱包
			goMyMoney(num) {
				uni.navigateTo({
					url: '../mySub/myWallet?data=' + num,
				});
			},
			//收藏
			CollectionFun() {
				uni.navigateTo({
					url: '../mySub/Collection'
				});
			},
			//签到操作
			userSigninFun() {
				if(this.token){
					this.$http.get('/api/user/userSignin').then((data) => {
						console.log(data.data.code)
						if (data.data.code == 200) {
							uni.showToast({
								title: '签到成功!',
								icon: "success",
								duration: 2000,
							});
							this.getUserinfo();
							this.userSignin();
						}
						
					}).catch((err)=>{
						
					})
				}else {
					uni.showToast({
						title: '请授权登录!',
						icon: "none",
						duration: 2000,
					});
				}
				
			},
			// 是否签到
			userSignin() {
				this.$http.get('/api/user/userSigninRule').then((data) => {
					if (data.data.code == 200) {
						this.isSignin = data.data.result.signinStatus
					}
				})
			},
			goDetail(id) {
				uni.navigateTo({
					url: '../mySub/bookDetail?id=' + id
				});
			},
			signOut(){
				this.userinfo = '';
				uni.clearStorage();
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/my.scss'
</style>
